﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace G.XNA
{
    /// <summary>
    /// 使用类似长方形的方式来绘制，使用指定的颜色来填充长方形，
    /// 在线段的接合处有空隙，非平滑，如果需要绘制平滑的线段，请使用RoundCappedLine
    /// </summary>
    public class LineRenderer
    {
        RenderTarget2D lineTexture;

        public LineRenderer(GraphicsDevice graphicsDevice)
        {
            lineTexture = new RenderTarget2D(graphicsDevice, 2, 3);//p234
            graphicsDevice.SetRenderTarget(lineTexture);
            graphicsDevice.Clear(Color.White);
            graphicsDevice.SetRenderTarget(null);
        }

        public void DrawLine(SpriteBatch spriteBatch,
                             Vector2 point1, Vector2 point2,
                             float thickness, Color color)
        {
            Vector2 difference = point2 - point1;//线段的向量
            float length = difference.Length();//线段长度
            spriteBatch.Draw(lineTexture, point1, null, color, difference.Angle(), new Vector2(0, 1), new Vector2(length / 2, thickness / 3), SpriteEffects.None, 0);
        }
    }
}
   